package com.cn.service.impl;

import com.cn.mapper.UserMapper;
import com.cn.entity.User;
import com.cn.service.UserService;
import com.cn.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param accountNumber 手机号
     * @param userPassword 密码
     * @return 用户信息
     */
    public User userLogin(String accountNumber, String userPassword){
        return userMapper.userLogin(accountNumber, userPassword);
    }

    /**
     * 查询一个用户的公开信息
     * @param id
     * @return
     */
    public User getUser(Long id){
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     *修改用户公开信息，未验证用户身份
     * @param user
     * @return
     */
    public boolean updateUserInfo(User user){
        return userMapper.updateByPrimaryKeySelective(user)==1;
    }

    public PageResult<User> getUserByStatus(int status, int page , int nums){
        // 使用PageHelper进行分页
        PageHelper.startPage(page, nums);
        List<User> list;
        if(status==0){
            list = userMapper.getNormalUserForPage();
        }else {
            list = userMapper.getBanUserForPage();
        }
        
        // 返回PageResult对象
        return new PageResult<>(new PageInfo<>(list).getTotal(), list);
    }


    /**
     * 通过用户账号查询用户信息
     *
     * @return*/
    @Override
    public PageResult<User> getUserByNumber(String searchValue, int page, int nums) {
        // 使用PageHelper进行分页
        PageHelper.startPage(page, nums);
        List<User> list = userMapper.getUserByNumberForPage(searchValue);
        
        // 返回PageResult对象
        return new PageResult<>(new PageInfo<>(list).getTotal(), list);
    }
}
